package org.openkoala.koala.codechecker;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.internal.resources.File;
import org.eclipse.core.internal.resources.Folder;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.MarkerUtilities;
import org.openkoala.koala.codechecker.vo.CodeCheckerVO;

/**
 * 
 * 
 *
 *
 * @author lingen
 *
 */
public class CodeCheckerAction implements IObjectActionDelegate {

	

	/**
	 * 构造函数
	 */
	public CodeCheckerAction() {
	}
	

	@Override
	public void run(final IAction action) {
		                IResource resource  = getResource();
						 try {
								IResource[] resources = null;
								if(resource instanceof IContainer){
									resources = ((IContainer) resource).members();
								}else{
									resources = new IResource[]{resource};
								}
								
								final List<File> files = getJavaFile(resources);
								ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
								monitorDialog.run(true, true, new IRunnableWithProgress() {
									
									
									@Override
									public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
										monitor.beginTask("开始扫描分析...", files.size());
										
										for(int i=0;i<files.size();i++){
											List<CodeCheckerVO> vos = new ArrayList<CodeCheckerVO>();
											monitor.worked(i+1);
											File file = files.get(i);
											monitor.setTaskName("Scanning file:"+file.getName());
											try {
												file.deleteMarkers("markers.codeChecker", true, IResource.DEPTH_INFINITE);
												vos = JavaCodeChecker.checkCode(file.getLocationURI().getPath());
												for (CodeCheckerVO vo:vos) {
													Map<String,Object> attributes = new HashMap();
													attributes.put(IMarker.MESSAGE, vo.getMessage());
													attributes.put(IMarker.LOCATION, "line " + vo.getLine());
													attributes.put(IMarker.LINE_NUMBER, vo.getLine());
													
													MarkerUtilities.createMarker(file, attributes, "KoalaCodeChecker.codeCheckerMarker");
												}
											} catch (CoreException e) {
												e.printStackTrace();
											}
											
										}
									}
								});
							} catch (CoreException e1) {
								e1.printStackTrace();
							} catch (InvocationTargetException e) {
								e.printStackTrace();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
		 
	}
	
	/**
	 * 锟斤拷取锟斤拷前选锟叫碉拷锟侥硷拷锟斤拷路锟斤拷
	 * 
	 * @return
	 */
	protected IResource getResource() {
		IStructuredSelection selection = 
				(IStructuredSelection)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
			Object element = selection.getFirstElement();
			IResource resources = null;
			if (element instanceof IResource) {
				resources = (IResource) element;
			}
			if (!(element instanceof IAdaptable)) {
				return null;
			}
			try{
				
			}catch(Exception e){
				e.printStackTrace();
			}
			IAdaptable adaptable = (IAdaptable) element;
			Object adapter = adaptable.getAdapter(IResource.class);
			resources = (IResource) adapter;
			return resources;
	}
	
	
	
	private List<File> getJavaFile(IResource[] resources) throws CoreException{
		
		List<File> files = new ArrayList<File>();
		for (IResource resource:resources) {
			if(resource instanceof File){
				if(resource.getFileExtension().equals("java"))files.add((File)resource);
			}else if(resource instanceof Folder){
				Folder folder = (Folder)resource;
				files.addAll(getJavaFile(folder.members()));
			}
		}
		return files;
	}
	

	@Override
	public void selectionChanged(final IAction action, final ISelection selection) {
		
	}
	
	

	@Override
	public void setActivePart(final IAction action, final IWorkbenchPart targetPart) {
	}

}
